package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfGoodsTypeDAO;
import com.ljkj.common.db.dao.ZjfGoodsTypePropertyDAO;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.domain.ZjfGoodsTypeProprerty;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfGoodsTypePropertyService {
	
	private ZjfGoodsTypePropertyDAO zjfGoodsTypePropertyDAO;
	private TLogService tLogService;

	/**
	 * 获取商品类型属性
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getGoodsTypePropertysByProperties(String typeId,int currentPage,int pageSize,String sortField,String sortOrder){
		 
		Class[] pojos = {ZjfGoodsTypeProprerty.class};
		String[] propertyNames= {"typeId"};
		Object[] values= {typeId};
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfGoodsTypePropertyDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfGoodsTypePropertyDAO.setPageMaxCount(pageSize);
		List<ZjfGoodsTypeProprerty> zjfGoodsTypeProprerties = this.zjfGoodsTypePropertyDAO.findByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfGoodsTypeProprerty zjfGoodsTypeProprerty : zjfGoodsTypeProprerties){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("typeId",zjfGoodsTypeProprerty.getTypeId());
			entry_map.put("propertyId",zjfGoodsTypeProprerty.getPropertyId());
			entry_map.put("propertyName", zjfGoodsTypeProprerty.getPropertyName());
			entry_map.put("propertyValue", zjfGoodsTypeProprerty.getPropertyValue());
			entry_map.put("propertySequence", zjfGoodsTypeProprerty.getPropertySequence());
			entry_map.put("createTime",zjfGoodsTypeProprerty.getCreateTime());
			entry_map.put("createUser", zjfGoodsTypeProprerty.getCreateUser());
			entry_map.put("updateTime", zjfGoodsTypeProprerty.getUpdateTime());
			entry_map.put("updateUser", zjfGoodsTypeProprerty.getUpdateUser());
			entry_map.put("disabledState", zjfGoodsTypeProprerty.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	

	/**
	 * 新增或更新用户组
	 * @param zdgCoupon
	 * @return
	 */
	public String addOrEdizjfGoodsTypePropertyInfo(ZjfGoodsTypeProprerty zjfGoodsTypeProprerty,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfGoodsTypeProprerty.getPropertyId() == null){
			
			zjfGoodsTypeProprerty.setPropertyId(RandomCode.makeCode());
			tlog.setOpType(1);
			tlog.setContent("新增商品类型属性："+ zjfGoodsTypeProprerty.getPropertyName());
			
			zjfGoodsTypeProprerty.setCreateTime(DateTime.getDateTime());
			zjfGoodsTypeProprerty.setCreateUser(tuser.getLoginName());
			if(this.saveNewzjfGoodsTypeProperty(zjfGoodsTypeProprerty)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfGoodsTypeProprerty.getDisabledState()!=null && zjfGoodsTypeProprerty.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("商品类型属性状态："+zjfGoodsTypeProprerty.getPropertyName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改商品类型属性："+zjfGoodsTypeProprerty.getPropertyName());
				}
				
				zjfGoodsTypeProprerty.setUpdateTime(DateTime.getDateTime());
				zjfGoodsTypeProprerty.setUpdateUser(tuser.getLoginName());
				this.zjfGoodsTypePropertyDAO.update(zjfGoodsTypeProprerty);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public Boolean saveNewzjfGoodsTypeProperty(ZjfGoodsTypeProprerty zjfGoodsTypeProprerty){
		try{
			this.zjfGoodsTypePropertyDAO.save(zjfGoodsTypeProprerty);
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
			return false;
		}
	}
	
	public ZjfGoodsTypeProprerty geZjfGoodsTypePropertyById(String typePropertyId){
		return this.zjfGoodsTypePropertyDAO.findById(typePropertyId);
	}
	

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}
	public void setZjfGoodsTypePropertyDAO(ZjfGoodsTypePropertyDAO zjfGoodsTypePropertyDAO) {
		this.zjfGoodsTypePropertyDAO = zjfGoodsTypePropertyDAO;
	}

}
